package com.demo.delivery.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.demo.delivery.R;
import com.demo.delivery.entity.User;
import com.demo.delivery.sqlite.BusinessResult;
import com.demo.delivery.sqlite.UserBusiness;
import com.demo.delivery.utils.CurrentUserUtils;
import com.demo.delivery.utils.ImmersionBarUtils;
import com.demo.delivery.utils.TitleBarUtils;

public class LoginActivity extends AppCompatActivity {

    private ImageView ivPasswordEye;

    private EditText etUsername, etPassword;

    private TextView tvRegister;

    private Button btnLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ImmersionBarUtils.immersion(this, true);
        TitleBarUtils.setTitle(this, "外卖订餐");
        bindView();
        initView();
    }

    private void bindView() {
        ivPasswordEye = findViewById(R.id.iv_password_eye);
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        tvRegister = findViewById(R.id.tv_register);
        btnLogin = findViewById(R.id.btn_login);
    }

    private void initView() {
        ivPasswordEye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransformationMethod method = etPassword.getTransformationMethod();
                if (method.equals(HideReturnsTransformationMethod.getInstance())) {
                    ivPasswordEye.setImageResource(R.drawable.ic_password_hide);
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    ivPasswordEye.setImageResource(R.drawable.ic_password_show);
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //登录
                User user = new User();
                user.setUsername(etUsername.getText().toString());
                user.setPassword(etPassword.getText().toString());
                BusinessResult<User> result = UserBusiness.login(user);
                Toast.makeText(LoginActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
                if (result.isSuccess()) {
                    //保存当前登录用户信息
                    CurrentUserUtils.setCurrentUser(result.getData());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                }
            }
        });
    }

    /**
     * 注册成功的数据返回至此
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            //是获取注册界面回传过来的用户名
            String username = data.getStringExtra("username");
            if (!TextUtils.isEmpty(username)) {
                //设置用户名到 et_username 控件
                etUsername.setText(username);
            }
        }
    }
}
